//
//  YSPStatusCell.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/21.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPStatusCell: UITableViewCell {
    
    /// 配图宽度约束
    var pictureViewWidthCon: NSLayoutConstraint?
    
    /// 配图高度约束
    var pictureViewHeightCon: NSLayoutConstraint?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusModel: YSPStatus? {
        didSet {
            topView.statusModel = statusModel
            
            contentLabel.text = statusModel?.text
            
            pictureView.statusModel = statusModel
            let viewSize = pictureView.getViewSize()
            pictureViewWidthCon?.constant = viewSize.width
            pictureViewHeightCon?.constant = viewSize.height
        }
    }
    
    ///返回行高
    func getRowHeightOfCell(statusModel: YSPStatus) -> CGFloat {
        self.statusModel = statusModel
        layoutIfNeeded()
        let bottomViewMaxY = CGRectGetMaxY(bottomView.frame)
        return bottomViewMaxY
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }

    
    func prepareUI() {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        
        let screenWidth = UIScreen.mainScreen().bounds.size.width
        
        //topView
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: screenWidth, height: 44))
        
        //contentLabel
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: screenWidth - 2 * 8))
        
        //pictureView
//        let constraint = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 400, height: 100), offset: CGPoint(x: 0, y: 8))
//        
//        pictureViewWidthCon = pictureView.ff_Constraint(constraint, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCon = pictureView.ff_Constraint(constraint, attribute: NSLayoutAttribute.Height)
        
        //bottomView
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: screenWidth, height: 44), offset: CGPoint(x: -8, y: 8))
    }
    
    //MARK: - 懒加载
    private lazy var topView: YSPTopView = YSPTopView()
    
    lazy var contentLabel: UILabel = {
       let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.blackColor()
        //设置到了多少长度之后就换行
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 2 * 8
        return label
    }()
    
    lazy var bottomView: YSPBottomView = YSPBottomView()
    
    lazy var pictureView: YSPPictureView = YSPPictureView()
    
    
    
    
}
